import Dialog from '../../../../lib/vant/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {
      "type": 1,//类型：1、4字组成成语  2、选择4字成语
      "list": [
        { "id": "1", "value": "像", "isCheck": false },
        { "id": "2", "value": "厌", "isCheck": false },
        { "id": "3", "value": "类", "isCheck": false },
        { "id": "4", "value": "班", "isCheck": false },
        { "id": "5", "value": "虎", "isCheck": false },
        { "id": "6", "value": "不", "isCheck": false },
        { "id": "7", "value": "谈", "isCheck": false },
        { "id": "8", "value": "烦", "isCheck": false },
        { "id": "9", "value": "犬", "isCheck": false },
        { "id": "10", "value": "就", "isCheck": false },
        { "id": "11", "value": "按", "isCheck": false },
        { "id": "12", "value": "画", "isCheck": false },
        { "id": "13", "value": "高", "isCheck": false },
        { "id": "14", "value": "其", "isCheck": false }
        ]
    },
    "checkList": [],//已选中的文字下标
    "kanPopupShow": false,//是否显示查看答案成功的弹窗
    "errorPopupShow": false,//是否显示回答错误的弹窗
    "successPopupShow": false,//是否显示回答正确的弹窗
    "rewardPopupShow": false,//是否显示获得奖励的弹窗
    "recordPopupShow": false,//是否显示战绩的弹窗
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 看一眼答案
   */
  kan: function(e){
    // 查看失败
    // this.DialogAlert("查看失败");
    // 查看成功
    var that = this;
    that.setData({
      "kanPopupShow": true
    })
  },

  /**
   * 关闭“看一眼答案”弹窗
   */
  closeKan: function(e){
    var that = this;
    that.setData({
      "kanPopupShow": false
    })
  },

  /**
   * 考验好友
   */
  testFriends: function () {
    this.DialogAlert("记得喊好友答题哦！");
  },

  /**
   * 跳过-2橙贝
   */
  skip: function(e){
    this.DialogAlert("-2橙贝");
  },

  /**
   * 分享
   */
  share: function(e){
    this.DialogAlert("分享失败");
  },

  /**
   * 数据点击
   */
  infoBindTap: function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    var type = e.currentTarget.dataset.type;
    switch (type) {
      case 1:
        var checkList = that.data.checkList;//已选中的文字下标
        if (checkList.length < 4) {
          var isCheck = that.data.info.list[index].isCheck;// 获取当前下标是否为选中状态
          if (!isCheck){
            that.setData({
              ["info.list[" + index + "].isCheck"]: true,
              ["checkList[" + checkList.length + "]"]: index
            });
          }
          if (checkList.length == 4){
            // 回答错误的弹窗
            /*that.setData({
              "errorPopupShow": true
            });*/
            // 回答正确的弹窗
            /*that.setData({
              "successPopupShow": true
            });*/
            // 获得奖励的弹窗
            that.setData({
              "rewardPopupShow": true
            });
          }
        }
        break;
      case 2:
        for (var i = 0; i < that.data.info.list.length; i++){
          if (i != index){
            that.setData({
              ["info.list[" + i + "].isCheck"]: false
            });
          }else{
            that.setData({
              ["info.list[" + i + "].isCheck"]: true,
              ["checkList[0]"]: index
            });
          }
        }
        break;
    }
  },

  /**
   * 数据删除：针对type为1的
   */
  infoBindTapClear: function (e) {
    var that = this;
    var checkList = that.data.checkList;
    var clearCheckListIndex = checkList.length - 1;
    var infoListIndex = checkList[clearCheckListIndex];
    checkList.splice(clearCheckListIndex,1);
    that.setData({
      ["info.list[" + infoListIndex + "].isCheck"]: false,
      "checkList": checkList
    });
  },

  /**
   * 回答错误的弹窗关闭
   */
  closeError: function (e) {
    var that = this;
    that.setData({
      "errorPopupShow": false
    });
  },

  /**
   * 回答正确的弹窗关闭
   */
  closeSuccess: function (e) {
    var that = this;
    that.setData({
      "successPopupShow": false
    });
  },

  /**
   * 奖励的弹窗关闭
   */
  closeReward: function (e) {
    var that = this;
    that.setData({
      "rewardPopupShow": false
    });
  },

  /**
   * 战绩的弹窗关闭
   */
  closeRecord: function (e) {
    var that = this;
    that.setData({
      "recordPopupShow": false
    });
  },

  /**
   * Dialog弹窗
   */
  DialogAlert: function(e){
    Dialog.alert({
      message: e,
      showConfirmButton: false,
      overlay: false
    });

    setTimeout(() => {
      Dialog.close();
    }, 1000);
  }
})